package com.competition.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.competition.entity.PlayAudit;
import com.competition.entity.PlayAuditOrder;
import com.competition.entity.PlayExperience;
import com.competition.mapper.PlayAuditMapper;
import com.competition.mapper.PlayAuditOrderMapper;
import com.competition.mapper.PlayExperienceMapper;
import com.competition.resp.ResponseResult;
import com.competition.service.PlayExperienceService;
import com.competition.utils.uuid.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;

@RestController
@RequestMapping("/playExperience")
public class PlayExperienceController {
    @Autowired
    private PlayExperienceMapper playExperienceMapper;
    @Autowired
    private PlayExperienceService playExperienceService;
    @Autowired
    private PlayAuditMapper playAuditMapper;
    @Autowired
    private PlayAuditOrderMapper playAuditOrderMapper;

    @GetMapping("/getAllPlayExperience")
    public ResponseResult getAllPlayExperience(@RequestParam("current") int current){
        if (current<0){
            return new ResponseResult(400,"参数异常",null);
        }
        try {
            Page<PlayExperience> playExperiencePage = playExperienceMapper.selectPage(new Page<>(current, 5), null);
            return new ResponseResult(200,"获取成功",playExperiencePage);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(400,"获取失败",null);
        }
    }
    @GetMapping("/isBooking")
    public ResponseResult isBooking(@RequestParam("openid") String openid,@RequestParam("playExperienceId") String playExperienceId){
        if (openid == null|| playExperienceId==null){
            return new ResponseResult(400,"参数异常",null);
        }
        try {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_id",openid);
            queryWrapper.eq("play_experience_id",playExperienceId);
            boolean exists = playAuditOrderMapper.exists(queryWrapper);
            if (exists){
                return new ResponseResult(200,"以报名",false);
            }else {
                return new ResponseResult(200,"未报名",true);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(400,"异常报错",null);
        }

    }
    @PostMapping("/addPlayExperience")//预约订单
    @Transactional(isolation = Isolation.READ_COMMITTED,rollbackFor = {Exception.class,RuntimeException.class,ClassCastException.class})
    public ResponseResult addPlayExperience(@RequestBody HashMap<String,Object> params) {

        if ( params.get("openid")== null || params.get("PlayExperienceId")== null ||  params.get("Date")== null ||params.get("Time") == null ||
               params.get("Descr")== null ||params.get("Name") == null || params.get("Phone")== null ){
            return new ResponseResult(400,"参数异常",null);
        }

        String uuid = UUIDUtil.getUUID();
        try {
            PlayAuditOrder playAuditOrder = new PlayAuditOrder();
            playAuditOrder.setUserId((String) params.get("openid"));
            playAuditOrder.setPlayAuditOrderId(uuid);
            playAuditOrder.setPlayExperienceId( Integer.parseInt((String) params.get("PlayExperienceId")));
            playAuditOrderMapper.insert(playAuditOrder);


            PlayAudit playAudit = new PlayAudit();
            SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
            playAudit.setPlayAuditId(uuid);
            playAudit.setPlayAuditDate(ft.parse((String) params.get("Date")) );
            DateTimeFormatter localTimeFormat = DateTimeFormatter.ofPattern("HH:mm:ss");
            LocalTime time = LocalTime.parse((String) params.get("Time"),localTimeFormat);
            playAudit.setPlayAuditTime(time);
            playAudit.setPlayAuditDescr((String) params.get("Descr"));
            playAudit.setPlayAuditName((String) params.get("Name"));
            playAudit.setPlayAuditPhone((String) params.get("Phone"));
            playAuditMapper.insert(playAudit);

            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("play_experience_id",params.get("PlayExperienceId"));
            PlayExperience playExperience = playExperienceMapper.selectOne(queryWrapper);


            UpdateWrapper updateWrapper = new UpdateWrapper();
            updateWrapper.set("play_experience_count",playExperience.getPlayExperienceCount()+1);
            updateWrapper.eq("play_experience_id",params.get("PlayExperienceId"));
            playExperienceService.update(updateWrapper);

            return new ResponseResult(200,"添加成功",null);

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
//            return new ResponseResult(400,"异常报错",null);
        }
    }

    @GetMapping("/delOrderById")
    public ResponseResult delById(@RequestParam("experienceId")String experienceId,@RequestParam("openid")String openid){
        if (experienceId == null|| openid ==null){
            return new ResponseResult(400,"参数异常",null);
        }
        try {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_id",openid);
            queryWrapper.eq("play_experience_id",experienceId);
            PlayAuditOrder playAuditOrder = playAuditOrderMapper.selectOne(queryWrapper);

            QueryWrapper queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("play_audit_id",playAuditOrder.getPlayAuditOrderId());
            playAuditMapper.delete(queryWrapper1);


            playAuditOrderMapper.delete(queryWrapper);

            return new ResponseResult(200,"删除成功",null);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    @GetMapping("/getPlayExperienceById")
    public ResponseResult getPlayExperienceById(@RequestParam("experienceId")String experienceId){
        if (experienceId == null){
            return new ResponseResult(400,"参数异常",null);
        }
        try {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("play_experience_id",experienceId);
            PlayExperience byId = playExperienceService.getOne(queryWrapper);
            return new ResponseResult(200,"获取成功",byId);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(400,"异常报错",null);
        }
    }
}
